package br.com.lh.urabuilder.command
{
	import br.com.lh.urabuilder.business.ViewManager;
	import br.com.lh.urabuilder.domain.xml.ConditionalAction;
	import br.com.lh.urabuilder.domain.xml.Option;
	import br.com.lh.urabuilder.event.SaveConditionalPropertiesEvent;
	import br.com.lh.urabuilder.util.pointchooser.TwoComponentPointChooser;
	import br.com.lh.urabuilder.view.ActionComponent;
	import br.com.lh.urabuilder.view.arrow.ArrowContainer;
	
	import com.adobe.cairngorm.control.CairngormEvent;
	
	import mx.collections.ArrayCollection;
	
	/**
	 * Save a conditional action, adding the text to the xml 
	 * and drawing the respective arrows.
	 * 
	 * 
	 * @author Luiz Henrique
	 * 
	 */
	public class SaveConditionalPropertiesCommand extends SavePropertiesCommand
	{
		public function SaveConditionalPropertiesCommand(){
			super();
		}
		
		public override function execute(event:CairngormEvent):void{
			var component:ActionComponent = ActionComponent(SaveConditionalPropertiesEvent(event).component);
			var action:ConditionalAction =ConditionalAction(component.uraAction);
			var optionList:ArrayCollection =SaveConditionalPropertiesEvent(event).optionList;
			updateXML(optionList,component);
			drawArrows(action.getOptions(),component);
		}
		
		private function updateXML(optionList:ArrayCollection,component:ActionComponent):void{
			ConditionalAction(component.uraAction).clearOptions();
			var conditionalAction:ConditionalAction =ConditionalAction(component.uraAction);
			for each (var option:Option in optionList){
				if (option !=null){
					conditionalAction.addOption(option);					
				}
			}
			super.doUpdate(component.uraAction);
		}
		
		private function drawArrows(optionList:ArrayCollection,component:ActionComponent):void{
			var viewManager:ViewManager = uraModelLocator.viewManager;
			var components:ArrayCollection = viewManager.getComponents();
			for each(var option:Option in optionList){
				var endComponent:ActionComponent =ActionComponent(components.getItemAt(option.nextId));
				uraModelLocator.arrowManager.insertBoudedArrow(component,endComponent,option.pattern);
			}
		}

		
	}
}